package Element;

/**
 *  Van Tan Tai
 *  3 -11 -2011
 */

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import Core.Event;
import Core.KEY;
import Core.RGBFont;
import Core.ScrollMe;

public class Listbox {
	
	private final short DY = 5; // khoang cach 2 Item
	private int HEI = 50;
	
	public class ItemList{
		String caption;
		//TODO: add your code here: image, text, etc...
		public ItemList(int h, String s) {
			HEI = h;
			//TODO: init here
			caption = s;
		}
		public void destroy(){
			//TODO: destroy them here
		}
		public void paint(Graphics g, int x, int y, boolean isFocus){
			//TODO: draw your think here
			g.setColor(isFocus ? 0xAABBCC : 0xCCAABB);
			g.fillRect(x, y, w, HEI);
			RGBFont.fnormal.drawString(g, caption, x + 15, y + 15, 0, 0);
		}
	}
	
	private ScrollMe list = null; 
	private Vector vec = null;
	private Event e = null;
	public int x, y, w, h; // bound
	public int iIndex = 0; // Index of Highlight row
	
	public Listbox(int ix, int iy, int iw, int ih, Event e) {
		x = ix; y = iy; w = iw; h = ih;
		list = new ScrollMe(w, 0, 2); // Speed here 0, 1, 2 , 3
		list.setList(null);
		vec = new Vector();
		list.setViewer(ix, iy, iw, ih);
		this.e = e;
	}
	
	public void addItem(ItemList item){
		vec.addElement(item);
		list.viewH += HEI + DY;
	}
	public void removeItem(int index){
		list.viewH -= HEI + DY;
		vec.removeElementAt(index);
	}
	
	public void paint(Graphics g){
		g.setColor(0);
		g.fillRect(x, y, w, h);
		g.setColor(0xFF0000);
		g.drawRect(x-1, y-1, w+1, h+1);
		list.setClip(g);
		int t = list.getY();
		//for(int t = list.getY() /*+3*/, i = 0; i < vec.size(); i++, t += HEI + DY){
		//	if(!list.isInner(t, HEI))
		//		continue;
			//ItemList item = (ItemList)vec.elementAt(i);
		//	((ItemList)vec.elementAt(i)).paint(g, x, t, iIndex == i);
		//}
		//System.out.println("ghash " + t);
		for(int i = 0; i < vec.size(); i++,t += HEI + DY){
					//if(!list.isInner(t, HEI))
						//continue;
					//ItemList item = (ItemList)vec.elementAt(i);
					((ItemList)vec.elementAt(i)).paint(g, x, t, iIndex == i);
				}
	}
	public void destroy(){
		list.destroy();
		list = null;
		for(int i = 0; i < vec.size(); i++)
			((ItemList)vec.elementAt(i)).destroy();
		vec.removeAllElements();
		vec = null;
		e = null;
	}
	public void onResize(){
		list.viewW = w;
		list.setViewer(x, y, w, h);
	}
	public void touchDrag(int x, int y) {
		list.move(x, y);
	}
	public void touchDown(int x, int y) {
		list.moveStart(x, y);
	}
	public void touchUp(int x, int y, int dragTime, int xBegin, int yBegin) {
		list.moveFinish(x, y, dragTime, xBegin, yBegin);
	}
	public void Click(int x, int y) {
		if(list.isInner(y, 0)){
			y = list.gotYin(y)/*-3*/;
			int index = y/(HEI + DY);
			//System.out.println("index="+index);
			if(index < 0 || index >= vec.size())
				return;
			if(x > this.x && y > index*(HEI+DY) && x < this.x + this.w && y < index*(HEI+DY) + HEI){
				if(this.iIndex == index){
					if(e != null) e.action();
				} else this.iIndex = index;
			}
		}
	}
	public void update(){
		list.update();
	}
	public void onkeyDown(int keyCode) {
		if(KEY.isPressed(KEY.DOWN)){
			if(iIndex < vec.size()-1){
				iIndex++;
				list.select(iIndex*(HEI + DY), HEI + DY);
			}
		}
		else if(KEY.isPressed(KEY.UP)){
			if(iIndex > 0){
				iIndex--; 
				list.select(iIndex*(HEI + DY), HEI + DY);
			}
		}
	}
}
